package com.qf.dao.impl;

import com.qf.dao.Card1Dao;
import com.qf.entity.Card1;
import com.qf.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class Card1DaoImpl implements Card1Dao {

    private QueryRunner queryRunner = new QueryRunner(JdbcUtils.ds);


    @Override
    public List<Card1> findAllCard1(Integer uid) {
//        c2 join t_user u on c2.uid = u.id
        String sql = "SELECT * FROM t_card1 WHERE uid = ?";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(Card1.class),uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int update(Card1 card1) {
        try {
            return queryRunner.update("update t_card1 set name = ?,phone = ?,password=?",card1.getName(),card1.getPhone(),card1.getPassword());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Card1 selectOneById(String id) {
        try {
            return queryRunner.query("select * from t_card1 where id = ?",new BeanHandler<>(Card1.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int add(Card1 card1) {
        String sql = "insert into t_card1 values (?,?,?,?,?,?,?)";
        try {
            return queryRunner.update(sql,card1.getId(),card1.getName(),card1.getPhone(),card1.getPassword(),card1.getType(),card1.getBalance(),card1.getUid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Card1 seleteOne(Card1 card1) {
        String sql = "select * from t_card1 where id=? and name=? and phone=? and password=? and uid=?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Card1.class),card1.getId(),card1.getName(),card1.getPhone(),card1.getPassword(),card1.getUid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Card1 seleteOne(String id1, String name, String phone, String password) {
        String sql = "select * from t_card1 where id=? and name=? and phone=? and password=?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Card1.class),id1,name,phone,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Card1 seleteOne(String id1, String name) {
        String sql = "select * from t_card1 where id=? and name=?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Card1.class),id1,name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Card1> selectByUid(String uid) {
        String sql = "select * from t_card1 where uid = ?";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(Card1.class),uid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int shoukuan(String id1, double money) {
        String sql = "update t_card1 set balance = balance + ? where id = ?";
        try {
            return queryRunner.update(sql,money,id1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int zhifu(String id1, double money) {
        String sql = "update t_card1 set balance = balance - ? where id = ?";
        try {
            return queryRunner.update(sql,money,id1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateBalanceByID(double num, String c1) {
        String sql = "update t_card1 set balance = balance - ? where id = ?";
        try {
            return queryRunner.update(sql,num,c1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(String id) {
        String sql = "delete from t_card1 where id = ?";
        try {
            return queryRunner.update(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
